/*
 * CS_LAST_WEEK_RANK_REWARD_V2.cpp
 *
 *  Created on: 2014. 1. 22.
 *      Author: mega
 */

#include "../../User.h"
#include "../../Command.h"
#include "../../NLogic/Reward.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

void SendLastWeekRewardFailV2(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_LAST_WEEK_RANK_REWARD_V2);
	pUser->Send(pAck);
}

void Command::CS_LAST_WEEK_RANK_REWARD_V2(User * pUser, ::Network::Packet * pPacket)
{
/*	/// 주간 랭킹 보상 받기(PVP 랭킹보상) : 랭킹 보상만 있음
	ZNO_CS_LAST_WEEK_RANK_REWARD_V2	0x280C
	{

	}

	ZNO_SC_LAST_WEEK_RANK_REWARD_V2	0x280D
	{
		s16	ack;
	}*/

	PREPARE_PACKET;

//	SendLastWeekRewardFailV2(pUser, ACK_NOT_EXIST_REWARD);
//	return;

	NLogic::Reward::Data * pRankRewardData = pUser->GetReward()->GetData(NLogic::Reward::RewardType::LAST_WEEK_RANK_REWARD);
	if (NULL == pRankRewardData)
	{
		DEBUGLOG("%s : pRankRewardData is NULL!", __FUNCTION__);
		SendLastWeekRewardFailV2(pUser, ACK_NOT_EXIST_REWARD);
		return;
	}

	time_t lastWeekRankRewardTime = pUser->GetUserInfo().array_Slot[static_cast<int>(NLogic::Reward::RewardType::LAST_WEEK_RANK_REWARD)].reg_date;
//	if (0 != pRankRewardData->_value || lastWeekRankRewardTime > NLogic::NFunction::GetWeekInitTime())
	if (lastWeekRankRewardTime > NLogic::NFunction::GetWeekInitTime())
	{
		DEBUGLOG("%s : Already receive reward!", __FUNCTION__);
		SendLastWeekRewardFailV2(pUser, ACK_NOT_EXIST_REWARD);
		return;
	}

//	if (pUser->GetUserInfo().score == 0)
//	{
//		DEBUGLOG("%s : Already receive reward!", __FUNCTION__);
//		SendLastWeekRewardFailV2(pUser, ACK_NOT_EXIST_REWARD);
//		return;
//	}

	NResource::WeeklyRewardData::DATATREE & clsRankRewardTree =
			NResource::Static::InstancePtr()->GetWeeklyReward()->GetFly(pUser)->GetFlyTree();

	bool bReward = false;
	NResource::WeeklyRewardData::Flyweight * pFly = NULL;

	auto rewardIter = clsRankRewardTree.begin();

	for (; rewardIter != clsRankRewardTree.end(); ++rewardIter)
	{
		pFly = rewardIter->second;

		if (NULL == pFly)
			continue;

		if (0 != pFly->_TYPE)
			continue;

//		if(pFly->_MIN <= pRankRewardData->_myRanking && pRankRewardData->_myRanking <= pFly->_MAX)
		if(static_cast<uint32_t>(pFly->_MIN) <= pUser->GetUserInfo().score && pUser->GetUserInfo().score <= static_cast<uint32_t>(pFly->_MAX))
		{
			bReward = true;
			break;
		}
	}

	if (5 > pUser->GetUserInfo().heart_blocked) // 참여 친구 조회 (5명 이상 보상 가능)
		bReward = false;

	if (false == bReward)
	{
		DEBUGLOG("%s : Can't receive reward! rank=%d", __FUNCTION__, pUser->GetUserInfo().score);
		SendLastWeekRewardFailV2(pUser, ACK_NOT_EXIST_REWARD);
		return;
	}

	// 보상 메일 발송
	NLogic::MailBox::Mail::Data clsMailData;
	clsMailData.send_user_seq		= 0;
	clsMailData.send_user_nickname	= "GAMEVIL";
	clsMailData.recv_user_seq		= pUser->GetUserSeq();
	clsMailData.message				= NResource::Static::InstancePtr()->GetMailMessageResource()->GetMessageWeeklyRankingReward();

	// 아이템 기본 정보 설정
	NLogic::Item::Data clsItemData;
	clsItemData.tid			= pFly->_REWARD_TID;
	clsItemData.sub_type	= pFly->_REWARD_SHEET_TYPE;
	clsItemData.class_type	= -1;
	NResource::Static::InstancePtr()->GetItemResource()->setItemEffect(clsItemData);

	// 아이템 타입 확인 (골드인지, 아이템인지...)
	int itemType = NResource::Static::InstancePtr()->GetItemResource()->GetItemSubType(clsItemData);

	if (static_cast<int>(ITEM_SUBTYPE_GOLD) == itemType)
	{
		clsMailData.gift_status		= EM_GIFT_GOLD;
		clsMailData.gold			= pFly->_VALUE;

		if (false == NLogic::NFunction::SendMail(pUser, clsMailData))
		{
			DEBUGLOG("%s : GOLD Mail Send Fail!!");
			SendLastWeekRewardFailV2(pUser, ACK_UNKNOWN_ERROR);
			return;
		}
	}
	else
	{
		int sendMailCount	= 0;

		// 스택 아이템인 경우
		if (1 == NResource::Static::InstancePtr()->GetItemResource()->getItemStackType(clsItemData))
		{
			clsItemData.quantity	= pFly->_VALUE;
			sendMailCount			= 1;
		}
		else
		{
			// 스택 아이템이 아닌 경우 하나씩 첨부하여 발송
			clsItemData.quantity	= 1;
			sendMailCount			= pFly->_VALUE;
		}

		// 아이템 부가 정보 세팅
		if (false == NResource::Static::InstancePtr()->GetItemResource()->SetItemDataFromResource(pUser, clsItemData))
		{
			DEBUGLOG("%s : SetItemDataFromResource Fail!", __FUNCTION__);
			SendLastWeekRewardFailV2(pUser, ACK_UNKNOWN_ERROR);
			return;
		}

		clsMailData.gift_status	= EM_GIFT_ITEM;
		clsMailData.item = clsItemData;

		// 메일 발송
		for (int i = 0; i < sendMailCount; i++)
		{
			if (false == NLogic::NFunction::SendMail(pUser, clsMailData))
			{
				DEBUGLOG("%s : ITEM Mail Send Fail!!");
				SendLastWeekRewardFailV2(pUser, ACK_UNKNOWN_ERROR);
				return;
			}
		}
	}

	// 보상 여부 갱신
	time_t curtime = time(NULL);

	pRankRewardData->_value = 1;
	pUser->GetUserInfo().array_Slot[static_cast<int>(NLogic::Reward::RewardType::LAST_WEEK_RANK_REWARD)].reg_date = curtime;
	pUser->FlushUserInfoCache();

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_LAST_WEEK_RANK_REWARD_V2);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
